/* eslint-disable max-depth */
/* eslint-disable @typescript-eslint/no-unused-expressions */
/* eslint-disable max-params */
// 单元格值变更处理是否只读
/**
 * @param {object} bindInfos 绑定数据源信息
 * @param {string} sheetName 当前活动sheet页名称
 * @param {number} col 列
 * @param {number} row 行
 * @param {boolean} blo 是否只读
 */
export const editeValue = (bindInfos: any, sheetName: any, col: any, row: any, blo: any) => {
    if (bindInfos[sheetName]) {
        // eslint-disable-next-line no-constant-condition
        if (bindInfos[sheetName][row]) {
            if (bindInfos[sheetName][row][col]) {
                bindInfos[sheetName][row][col] = {
                    ...bindInfos[sheetName][row][col],
                    readOnly: blo,
                };
            } else {
                bindInfos[sheetName][row][col] = {
                    readOnly: blo,
                };
            }
        } else {
            bindInfos[sheetName][row] = {};
            bindInfos[sheetName][row][col] = {
                readOnly: blo,
            };
        }
    } else {
        bindInfos[sheetName] = {};
        bindInfos[sheetName][row] = {};
        bindInfos[sheetName][row][col] = {
            readOnly: blo,
        };
    }
    bindInfos[sheetName][row][col] = {
        ...bindInfos[sheetName][row][col],
        readOnly: blo,
    };
    return bindInfos;
};
// 点击当前单元格是否只读
export const readOnlyFn = (bindInfo: any, sheet: any, selections: any, setReadOnly: any) => {
    const bindInfos = JSON.parse(bindInfo ? JSON.stringify(bindInfo) : '{ }');
    const sheetName = sheet.name();
    if (selections[0]) {
        let flag = true;
        for (let i = 0; i < selections[0].rowCount; i++) {
            const row =
                selections[0].row === -1 ? selections[0].row + i + 1 : selections[0].row + i;
            for (let g = 0; g < selections[0].colCount; g++) {
                const col =
                    selections[0].col === -1 ? selections[0].col + g + 1 : selections[0].col + g;
                const dataObj = bindInfos[sheetName]?.[row]?.[col];
                flag = dataObj ? (!dataObj.readOnly ? false : flag) : false;
            }
        }
        setReadOnly(flag);
    }
};
// 设置只读
export const onReadOnly = (
    bindInfo: any,
    sheetName: any,
    selections: any,
    readOnly: boolean,
    setBindInfo: any
) => {
    let bindInfos = JSON.parse(JSON.stringify(bindInfo));
    if (bindInfos) {
        if (bindInfos[sheetName]) {
            for (let i = 0; i < selections[0].rowCount; i++) {
                const row =
                    selections[0].row === -1 ? selections[0].row + i + 1 : selections[0].row + i;
                for (let g = 0; g < selections[0].colCount; g++) {
                    const col =
                        selections[0].col === -1
                            ? selections[0].col + g + 1
                            : selections[0].col + g;
                    if (bindInfos[sheetName]?.[row]?.[col]) {
                        bindInfos[sheetName][row][col] = {
                            ...bindInfos[sheetName]?.[row]?.[col],
                            readOnly: !readOnly,
                        };
                    } else {
                        if (bindInfos[sheetName][row]) {
                            bindInfos[sheetName][row][col] = {};
                            bindInfos[sheetName][row][col] = {
                                readOnly: !readOnly,
                            };
                        } else {
                            bindInfos[sheetName][row] = {};
                            bindInfos[sheetName][row][col] = {
                                readOnly: !readOnly,
                            };
                        }
                    }
                }
            }
        } else {
            bindInfos[sheetName] = {};
            for (let i = 0; i < selections[0].rowCount; i++) {
                const row =
                    selections[0].row === -1 ? selections[0].row + i + 1 : selections[0].row + i;
                bindInfos[sheetName][row] = {};
                for (let g = 0; g < selections[0].colCount; g++) {
                    const col =
                        selections[0].col === -1
                            ? selections[0].col + g + 1
                            : selections[0].col + g;
                    bindInfos[sheetName][row][col] = {
                        readOnly: !readOnly,
                    };
                }
            }
        }
    } else {
        bindInfos = {};
        bindInfos[sheetName] = {};
        for (let i = 0; i < selections[0].rowCount; i++) {
            const row =
                selections[0].row === -1 ? selections[0].row + i + 1 : selections[0].row + i;
            bindInfos[sheetName][row] = {};
            for (let g = 0; g < selections[0].colCount; g++) {
                const col =
                    selections[0].col === -1 ? selections[0].col + g + 1 : selections[0].col + g;
                bindInfos[sheetName][row][col] = {
                    readOnly: !readOnly,
                };
            }
        }
    }
    setBindInfo(bindInfos);
};
// 行变更事件函数
export const rowChanged = (bindInfo: any, info: any, setBindInfo: any) => {
    const binds = JSON.parse(bindInfo ? JSON.stringify(bindInfo) : '{ }');
    if (binds[info.sheetName]) {
        const key = Object.keys(binds[info.sheetName]);
        info.propertyName !== 'addColumns' ? delete binds[info.sheetName][info.row] : null;
        const changeKey = key.filter((item) => {
            return info.propertyName === 'addRows' ? item >= info.row : item > info.row;
        });
        const newkeyData: any = {};
        changeKey.forEach((el) => {
            const newKey =
                info.propertyName === 'addRows' ? Number(el) + info.count : Number(el) - info.count;
            let obj = JSON.parse(JSON.stringify(binds[info.sheetName][el]));
            delete binds[info.sheetName][el];
            if (!Number.isNaN(newKey)) {
                newkeyData[newKey] = obj;
            }
        });
        binds[info.sheetName] = {
            ...binds[info.sheetName],
            ...newkeyData,
        };
        setBindInfo(binds);
    }
};
// 列变更事件函数
export const colChanged = (bindInfo: any, info: any, setBindInfo: any) => {
    const binds = JSON.parse(bindInfo ? JSON.stringify(bindInfo) : '{ }');
    if (binds[info.sheetName]) {
        const key = Object.keys(binds[info.sheetName]);
        const keyArr: any = {};
        key.forEach((item) => {
            keyArr[item] = Object.keys(binds[info.sheetName][item]);
        });
        key.forEach((item) => {
            // eslint-disable-next-line @typescript-eslint/no-unused-expressions
            info.propertyName !== 'addColumns'
                ? delete binds[info.sheetName][item][info.col]
                : null;
            const changeKey = keyArr[item].filter((el: any) => {
                return info.propertyName === 'addColumns' ? el >= info.col : el > info.col;
            });
            const newkeyData: any = {};
            changeKey.forEach((al: any) => {
                const newKey =
                    info.propertyName === 'addColumns'
                        ? Number(al) + info.count
                        : Number(al) - info.count;
                let obj = JSON.parse(JSON.stringify(binds[info.sheetName][item][al]));
                delete binds[info.sheetName][item][al];
                if (!Number.isNaN(newKey)) {
                    newkeyData[newKey] = obj;
                }
            });
            binds[info.sheetName][item] = {
                ...binds[info.sheetName][item],
                ...newkeyData,
            };
        });
        setBindInfo(binds);
    }
};
// 绑定数据源
export const bindData = (
    sheet: any,
    bindInfo: any,
    tableName: any,
    databaseName: string,
    fieldName: string,
    column: string,
    value: string,
    setBindInfo: any
) => {
    const selections = sheet.getSelections();
    const sheetName = sheet.name();
    let bindInfos = JSON.parse(JSON.stringify(bindInfo));
    const row = selections[0].row;
    const col = selections[0].col;
    if (bindInfos) {
        if (bindInfos[sheetName]) {
            // eslint-disable-next-line no-constant-condition
            if (bindInfos[sheetName][row]) {
                bindInfos[sheetName][row][col] = {
                    tableName,
                    databaseName,
                    fieldName,
                    filter: {
                        column,
                        value,
                    },
                    readOnly: true,
                };
            } else {
                bindInfos[sheetName][row] = {};
                bindInfos[sheetName][row][col] = {
                    tableName,
                    databaseName,
                    fieldName,
                    filter: {
                        column,
                        value,
                    },
                    readOnly: true,
                };
            }
        } else {
            bindInfos[sheetName] = {};
            bindInfos[sheetName][row] = {};
            bindInfos[sheetName][row][col] = {
                tableName,
                databaseName,
                fieldName,
                filter: {
                    column,
                    value,
                },
                readOnly: true,
            };
        }
        bindInfos[sheetName][row][col] = {
            tableName,
            databaseName,
            fieldName,
            filter: {
                column,
                value,
            },
            readOnly: true,
        };
    } else {
        bindInfos = {};
        bindInfos[sheetName] = {};
        bindInfos[sheetName][row] = {};
        bindInfos[sheetName][row][col] = {
            tableName,
            databaseName,
            fieldName,
            filter: {
                column,
                value,
            },
            readOnly: true,
        };
    }
    setBindInfo(bindInfos);
};
